package com.wh1200.mybatis.xmlless.util

import com.wh1200.mybatis.xmlless.dialect.DialectFactory
import com.wh1200.mybatis.xmlless.enums.Operations

/**
 *
 * @author 吴昊
 * @date 2023/12/18 9:56
 * @since v4.0.0
 * @version 1.0
 */
/**  in条件表达式模板 */
internal const val IN_TEMPLATE = "%s\n  %S\n%s"

/**  not null或者is not null的条件表达式模板 */
internal const val NO_VALUE = "%s %S"

/**
 * 获取条件操作符的表达式模板
 *
 * @return
 */
fun Operations.getTemplate(): String {
  val dialect = DialectFactory.getDialect(DbTypeHolder.getDBType())
  return when (this) {
    Operations.LtDate, Operations.LteDate,
    Operations.NeDate, Operations.GtDate,
    Operations.GteDate,
    Operations.EqDate      -> dialect.getEqDateConditionTemplate()

    Operations.EqMonth     -> dialect.getEqMonthConditionTemplate()
    Operations.Contains    -> "JSON_CONTAINS(%s, '$', %s%s)"
    Operations.ContainsAny -> "JSON_OVERLAPS(%s, %s%s)"
    Operations.Like        -> "%s %s CONCAT('%%', %s,'%%')"
    Operations.IlikeLeft,
    Operations.LikeLeft    -> "%s %s CONCAT('%%', %s)"

    Operations.IlikeRight,
    Operations.LikeRight   -> "%s %s CONCAT(%s, '%%')"

    Operations.NotIn,
    Operations.In          -> IN_TEMPLATE

    in listOf(
        Operations.NotNull, Operations.IsNotNull,
        Operations.IsNull, Operations.EqTrue,
        Operations.EqFalse, Operations.NotTrue,
        Operations.True
    )                      -> NO_VALUE

    Operations.Between     -> "%s %s %s AND %s"
    else                   -> "%s %s %s"
  }
}

fun Operations.getValueTemplate(): String {
  return getTemplate()
}
